import WebStorageCache from 'web-storage-cache'
import { LOCAL_STORAGE_NAME, HEADER_NAME } from '@utils/const'

export const wsCache = new WebStorageCache({
  storage: 'localStorage',
})

//设置AppId
export const setAppId = (value: number | string) => {
  wsCache.set(LOCAL_STORAGE_NAME.APPID, value)
}

//获取AppId
export const getAppId = (): string => {
  return wsCache.get(LOCAL_STORAGE_NAME.APPID)
}

//设置Token
export const setToken = (value: string) => {
  wsCache.set(LOCAL_STORAGE_NAME.Authorization, value)
}

//获取Token
export const getToken = (): string => {
  return wsCache.get(LOCAL_STORAGE_NAME.Authorization)
}

//设置OpenId
export const setOpenId = (value: string) => {
  wsCache.set(LOCAL_STORAGE_NAME.OPENID, value)
}

//获取OpenId
export const getOpenId = () => {
  return wsCache.get(LOCAL_STORAGE_NAME.OPENID)
}

//获取用户信息
export const getUserInfo = () => {
  return wsCache.get(LOCAL_STORAGE_NAME.USER_INFO)
}

//设置用户信息
export const setUserInfo = (value: {}) => {
  return wsCache.set(LOCAL_STORAGE_NAME.USER_INFO, value)
}

//设置UserId
export const setUserId = (value: string) => {
  wsCache.set(LOCAL_STORAGE_NAME.USERID, value)
}

//获取UserId
export const getUserId = () => {
  return wsCache.get(LOCAL_STORAGE_NAME.USERID)
}

//清除Token
export const clearToken = () => {
  wsCache.delete(LOCAL_STORAGE_NAME.Authorization)
}

//获取默认请求头
export const getDefaultHeader = () => {
  return {
    [HEADER_NAME.Authorization]: getToken(),
    [HEADER_NAME.APPID]: getAppId(),
    [HEADER_NAME.OPENID]: getOpenId(),
  }
}
